﻿using PSI.Helper;
using PSI.Services;
using PSI.Services.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class InPlanController : PSIController {

        public ActionResult Index() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.IN_PLAN)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            ViewBag.ConsignationBillNumber = this.Request.Params["billNumber"];
            ViewBag.PlanNumber = this.Request.Params["planNumber"];

            return View();
        }

        [HttpPost]
        public ActionResult GetInPlanListData() {
            var data = new InPlanService().GetInPlanListData(this.Request.Form);

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetTodayPlanListData() {
            var data = new InPlanReportService().GenReportCurrentMonth();

            return Json(data);
        }

        public ActionResult MakePlanIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.IN_PLAN_MAKE_PLAN)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult MakePlan() {
            var result = new InPlanService().MakePlan(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CancelPlan(string id) {
            var result = new InPlanService().CancelPlan(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CancelTruck(string id) {
            var result = new InPlanService().CancelTruck(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult ChangeDispatcher() {
            var result = new InPlanService().ChangeDispatcher(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult SendTruck() {
            var result = new InPlanService().SendTruck(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetSendTruckInfo(string id) {
            var data = new InPlanService().GetSendTruckInfo(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetTraceInfo(string id) {
            var data = new InPlanService().GetTraceInfo(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetTraceComments(string id) {
            var data = new InPlanService().GetTraceComments(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult Trace() {
            var result = new InPlanService().Trace(this.Request.Form);

            return Json(result);
        }

        public ActionResult Excel() {
            var fileName = new InPlanService().Excel(this.Request.Params, Server.MapPath("/"));

            return File(fileName, "application/vnd.ms-excel", "进口计划.xls");
        }

        [HttpPost]
        public ActionResult GetMakePlanListData() {
            var data = new InPlanService().GetMakePlanListData();

            return Json(data);
        }

        public ActionResult SendTruckIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.IN_PLAN_SEND_TRUCK)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetInPlanSendTruckListData() {
            var data = new InPlanService().GetSendTruckListData(this.Request.Form);

            return Json(data);
        }

        public ActionResult TraceIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.IN_PLAN_TRACE)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetTraceListData() {
            var data = new InPlanService().GetTraceListData();

            return Json(data);
        }

        [HttpPost]
        public ActionResult Finish(string id) {
            var result = new InPlanService().Finish(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult TraceComments() {
            var result = new InPlanService().TraceComments(this.Request.Form);

            return Json(result);
        }

        public ActionResult EvaluateService() {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        /// <summary>
        /// 进口计划汇总表
        /// </summary>
        /// <returns></returns>
        public ActionResult ImprotPlanReport()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        [HttpPost]
        public ActionResult GetImReport()
        {
            var dt = Request["dt"];

            var data = new InPlanReportService().GetJKJHHZB(dt);

            return Json(new { Data = data });
        }


        public ActionResult CompositeReport()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }
        [HttpPost]
        public ActionResult GetCompositeReport()
        {
            try
            {
                var dt = Request["dt"];

                var data = new InPlanReportService().ImANDEx(dt);

                return Json(new { Data = data });
            }
            catch (Exception ex)
            {
                return Json("");
            }
        }
    }
}
